/*
 * name:手机号码 Phone
 * author:yanmu.wj@taobao.com
 * date:2011-11-22
 */
KISSY.add('Phone',function(S,PhoneCore,Tip){
    var Phone = function(cfg){
        this.input = cfg.input && S.one(cfg.input);
        this.tip = (cfg.tip && S.one(cfg.tip)) ? Tip(cfg.tip) : null;
        this.on = cfg.on || 'keyup';
        this.defaultTip = cfg.defaultTip || null; //{tyep:"ok",msg:{title:'',content:''}}

        this.disabledMsg = S.isString(cfg.disabledMsg) ? cfg.disabledMsg : '手机号码格式有误，请重新输入！';
        this.disabled = true;
    };
    var _Phone = function(cfg){
        return new Phone(cfg);
    };
    S.augment(Phone,{
        init:function(){
            if(!this.input || !this.tip) return this;

            var self = this;
            this.input.on(this.on,function(){
                self.check();
            });

            return this;
        },
        check:function(){
            if(this.isEmpty()){
                this.disabled = true;
                this.resetTip();
            }else if(PhoneCore(S.trim(this.input.val())).check('cn')){
                //目前只支持中国大陆手机号码
                this.tip.ok();
                this.disabled = false;
            }else{
                this.tip.error('手机号码格式有误，请重新输入！');
                this.disabled = true;
            }
        },
        isEmpty:function(){
            return !S.trim(this.input.val()).length;
        },
        resetTip:function(){
            if(!this.tip) return;
            if(this.defaultTip && this.defaultTip.type && this.defaultTip.msg){
                this.tip.change(this.defaultTip.type,this.defaultTip.msg);
            }else{
                this.tip.hide();
            }
        }
    });

    return _Phone;
},{
    requires:['PhoneCore','Message']
})
